<?php
ob_start("ob_gzhandler");
unset($where, $mac, $submit, $Table, $_Fld1, $Fld2, $Fld3, $Fld4, $Fld5, $Fld6, $Fld7, $ID);

require_once('db_functions.php');

$mac = $_GET['mac'];
$where = $_GET['where'];

if($where == '') $where = 'list';
if(!$mac == '') $conf_mode = 'Phone';

$DBData = array($_POST['Fld1'], $_POST['Fld2'], $_POST['Fld3'], $_POST['Fld4'], $_POST['Fld5'], $_POST['Fld6'], $_POST['Fld7'], );
if($_POST['submit'] == 'Add') addtodb($_POST['Table'], $DBData);
if($_POST['submit'] == 'Update') updatedb($_POST['Table'], $DBData, $_POST['ID']);


// Sortier
if($where == 'list')
	{
		$content = "\n<table>\n";
		$content .= "<tr>\n<th width=\"12%\">MAC</th><th width=\"5%\">Ext.</th><th>User</th><th>Position</th><th width=\"8%\">Team</th><th width=\"8%\">Room</th><th>Email</th><th width=\"10%\">Settings</th>\n</tr>";
		dbconnect();
		$sql = "SELECT * FROM Phones ORDER BY Extension ASC";
		$dbresult = mysql_query($sql) or die($sql);
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
				$content .= "<tr>\n<td><a href=\"{$_SERVER['PHP_SELF']}?where=edit&amp;ID={$dbrow['ID']}\">{$dbrow['MAC']}</a></td>";
				$content .= "<td>{$dbrow['Extension']}</td>";
				$content .= "<td>{$dbrow['User']}</td>";
				$content .= "<td>{$dbrow['Position']}</td>";
				$content .= "<td>{$dbrow['Team']}</td>";
				$content .= "<td>{$dbrow['Location']}</td>";
				$content .= "<td><a href=\"mailto:{$dbrow['EMail']}\">{$dbrow['EMail']}</a></td>";
				$content .= "<td><a href=\"{$_SERVER['PHP_SELF']}?where=settings&amp;ID={$dbrow['ID']}\">Edit</a></td></tr>\n";
			}
		$content .= "<tr><td colspan=\"8\" border=\"0px\"><br /></td></tr>\n";
		$content .= "<tr>\n";
		$content .= "<td><form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
		$content .= "<input type=\"text\" name=\"Fld1\" value=\"\" size=\"10\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld2\" value=\"\" size=\"6\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld3\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld4\" value=\"\" size=\"15\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld5\" value=\"\" size=\"10\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld6\" value=\"\" size=\"10\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld7\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"hidden\" name=\"Table\" value=\"Phones\" /><input type=\"submit\" name=\"submit\" value=\"Add\" /></form></td>\n";
		$content .= "</tr>\n";
		$content .= "</table>\n";
	}
elseif($where == 'edit')
	{
		dbconnect();
		$sql = "SELECT * FROM Phones WHERE ID = " . $_GET['ID'];
		$dbresult = mysql_query($sql) or die($sql);
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
				$content .= "<a href=\"{$_SERVER['PHP_SELF']}\">Back</a>\n";
				$content .= "<table><tr>\n";
				$content .= "<td><form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\"><input type=\"hidden\" name=\"ID\" value=\"{$dbrow['ID']}\"  />";
				$content .= "<input type=\"text\" name=\"Fld1\" value=\"{$dbrow['MAC']}\" size=\"10\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld2\" value=\"{$dbrow['Extension']}\" size=\"6\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld3\" value=\"{$dbrow['User']}\" size=\"20\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld4\" value=\"{$dbrow['Position']}\" size=\"15\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld5\" value=\"{$dbrow['Team']}\" size=\"10\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld6\" value=\"{$dbrow['Location']}\" size=\"10\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld7\" value=\"{$dbrow['EMail']}\" size=\"20\" /></td>\n";
				$content .= "<td><input type=\"hidden\" name=\"Table\" value=\"Phones\" /><input type=\"submit\" name=\"submit\" value=\"Update\" /></form></td>\n";
				$content .= "</tr></table>\n";
				$content .= "<a href=\"{$_SERVER['PHP_SELF']}?where=delete&amp;Table=Phones&amp;ID={$dbrow['ID']}\">Delete This Phone</a>\n";
			}
	}
elseif($where == 'settings')
	{
		dbconnect();
		$sql = "SELECT * FROM Settings WHERE Phone = " . $_GET['ID'];
		$dbresult = mysql_query($sql) or die($sql);
		$content .= "<a href=\"{$_SERVER['PHP_SELF']}\">Back</a>\n";
		$content .= "<table>\n";
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
				$content .= "<tr>\n<td><form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\"><input type=\"hidden\" name=\"Fld1\" value=\"{$dbrow['Phone']}\" /><input type=\"hidden\" name=\"ID\" value=\"{$dbrow['ID']}\" />";
				$content .= "<input type=\"text\" name=\"Fld2\" value=\"{$dbrow['Keyword']}\" size=\"20\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld3\" value=\"{$dbrow['Value']}\" size=\"20\" /></td>\n";
				$content .= "<td><input type=\"text\" name=\"Fld4\" value=\"{$dbrow['Note']}\" size=\"20\" /></td>\n";
				$content .= "<td><input type=\"hidden\" name=\"Table\" value=\"Settings\" /><input type=\"submit\" name=\"submit\" value=\"Update\" /></form></td>\n";
				$content .= "<td><a href=\"{$_SERVER['PHP_SELF']}?where=delete&amp;Table=Settings&amp;ID={$dbrow['ID']}\">Delete</a>\n</td></tr>\n";
			}
		$content .= "<tr>\n";
		$content .= "<td><form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\"><input type=\"hidden\" name=\"Fld1\" value=\"{$_GET['ID']}\" />";
		$content .= "\n<input type=\"text\" name=\"Fld2\" value=\"\" size=\"20\" />\n";
		$content .= "</td>\n<td><input type=\"text\" name=\"Fld3\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld4\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"hidden\" name=\"Table\" value=\"Settings\" /><input type=\"submit\" name=\"submit\" value=\"Add\" /></form></td>\n";
		$content .= "</tr></table>\n";

		$content .= "<hr /><table>\n<tr>\n";
		$content .= "<td><form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";
		$content .= "<input type=\"text\" name=\"Fld1\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"text\" name=\"Fld2\" value=\"\" size=\"20\" /></td>\n";
		$content .= "<td><input type=\"hidden\" name=\"Table\" value=\"SNOM\" /><input type=\"submit\" name=\"submit\" value=\"Add\" /></form></td>\n";
		$content .= "</tr></table>\n";
	}
elseif($where == 'cycledb') cycledb();
elseif($where == 'delete') deletefromdb($_GET['Table'], $_GET['ID']);
else
	{
		// How did you get here
	}

$html = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$html .= "<html>\n\t<head>\n\t\t";
$html .= "<title>Phones</title>\n<link rel=\"stylesheet\" href=\"css.css\" type=\"text/css\" />\n</head>\n";
$html .= "<body>\n{$content}\n<br /><a href=\"index.php?where=cycledb\">.</a></body></html>\n";

if($conf_mode == "Phone")
	{
		// ob_end_clean();
		echo "<html>\n<pre>\n";
		dbconnect();
		$sql = "SELECT ID FROM Phones WHERE MAC = '{$mac}'";
		$dbresult = mysql_query($sql) or die($sql);
		$dbrow = mysql_fetch_array($dbresult);
		$ID = $dbrow['ID'];
		$sql = "SELECT * FROM Settings WHERE Phone = Default";
		$dbresult = mysql_query($sql) or die($sql);
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
				echo "# {$dbrow['Note']}\n{$dbrow['Key']}: {$dbrow['Value']}\n";
			}
		$sql = "SELECT * FROM Settings WHERE Phone = " . $ID;
		$dbresult = mysql_query($sql) or die($sql);
		while($dbrow = mysql_fetch_assoc($dbresult))
			{
				echo "# {$dbrow['Note']}\n{$dbrow['Key']}: {$dbrow['Value']}\n";
			}
		echo "</pre>\n<html>";
	}
else
	{
		echo $html;
	}
?>